package com.shop.front.service.store;

import com.shop.domain.store.StoreOrder;
import com.shop.front.consts.FrontConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional
public class RedisOrderService extends KeyExpirationEventMessageListener {



      @Autowired
      StoreOrderService storeOrderService;

      public RedisOrderService(RedisMessageListenerContainer container){
          super(container);
      }



      @Override
      public void onMessage(Message message,byte [] bytes){
            String expireKey = message.toString();

            if(expireKey.contains(FrontConst.USER_PAY_SOTRE_ORDER)){
                  String orderNo = expireKey.substring(expireKey.lastIndexOf(":")+1);
                  if(StringUtils.isNotBlank(orderNo)){
                        storeOrderService.closeStoreOrderNotPay(orderNo);
                  }

            }

      }


}
